package com.mccree.elasticsearch.utils;

import com.mccree.elasticsearch.entity.Goods;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 爬虫工具类
 * @Author: MccRee
 * @Date: 2022/5/24 11:40
 */
@Slf4j
public class HtmlParseUtil {


    public static List<Goods> parseJD(String keyWords){
        try {
            String url = "https://search.jd.com/Search?keyword="+keyWords;
            Document document = Jsoup.parse(new URL(url), 3000);
            Element element = document.getElementById("J_goodsList");
            if (null != element){
                Elements elements = element.getElementsByTag("li");
                List<Goods> goodsList = new ArrayList<>();
                for (Element el : elements){
                    String price = el.getElementsByClass("p-price").eq(0).text();
                    String name = el.getElementsByClass("p-name").eq(0).text();
                    String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
                    Goods goods = new Goods(name, price, img);
                    goodsList.add(goods);
                }
                return goodsList;
            }
            return null;
        } catch (IOException e) {
            log.error("IO_ERROR:",e);
            return null;
        }
    }

}
